package com.szh.mysobproject.ui.fragment

import android.annotation.SuppressLint
import androidx.fragment.app.viewModels
import by.kirich1409.viewbindingdelegate.viewBinding
import com.google.android.material.badge.BadgeUtils
import com.szh.mysobproject.R
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.app.AppFragment
import com.szh.mysobproject.ui.activity.*
import com.szh.mysobproject.app.TitleBarFragment
import com.szh.mysobproject.databinding.MyMeFragmentBinding
import com.szh.mysobproject.manager.UserManager
import com.szh.mysobproject.util.*
import com.szh.mysobproject.viewmodel.MsgViewModel

/**
 * @author : create by  szh
 * @date : 2022/6/17 10:25
 * @desc :个人中心界面
 */

@SuppressLint("UnsafeOptInUsageError")
class MyMeFragment : TitleBarFragment<AppActivity>() {

    private val mBinding: MyMeFragmentBinding by viewBinding()
    private val mMsgViewModel by viewModels<MsgViewModel>()

    private val badgeDrawable by lazy {
        createDefaultStyleBadge(requireContext(), 0).apply {
            val meContent = mBinding.meContent
            BadgeUtils.attachBadgeDrawable(this, meContent.ivMsgCenter)
        }
    }

    override fun getLayoutId(): Int = R.layout.my_me_fragment

    override fun onFragmentResume(first: Boolean) {
        super.onFragmentResume(first)
        val meContent = mBinding.meContent
        checkToken {
            val userBasicInfo = it.getOrNull()
            meContent.imageAvatar.loadAvatar(UserManager.currUserIsVip(), userBasicInfo?.avatar)
            meContent.textNickName.text = userBasicInfo?.nickname ?: "账号未登录"
        }
        mMsgViewModel.getUnReadMsgCount().observe(this) {
            val unReadCount = it.getOrNull() ?: return@observe
            badgeDrawable.isVisible = unReadCount.hasUnReadMsg
        }
    }

    override fun initEvent() {
        val meContent = mBinding.meContent
        //用户中心
        meContent.llUserInfoContainer.setFixOnClickListener {
            takeIfLogin {
                requireContext().startActivity<UserCenterActivity>()
            }
        }
        //富豪榜
        meContent.richListContainer.setFixOnClickListener {
            requireContext().startActivity<RichListActivity>()
        }
        //消息中心
        meContent.messageCenterContainer.setFixOnClickListener {
            requireContext().startActivity<MessageCenterActivity>()
        }
        //创作中心
        meContent.creationCenterContainer.setFixOnClickListener {
            requireContext().startActivity<CreationCenterActivity>()
        }
        //天气
        meContent.weatherContainer.setFixOnClickListener {
            //TODO:跳转到天气
        }
        //反馈中心
        meContent.feedbackContainer.setFixOnClickListener {
            //TODO:跳转到反馈中心
        }
        //设置页
        meContent.settingContainer.setFixOnClickListener {
            requireContext().startActivity<SettingActivity>()
        }

        meContent.videoPlay.setFixOnClickListener {
            requireContext().startActivity<MyVideoPlay>()
        }
    }

    override fun initData() {}

    override fun initView() {}

    override fun isStatusBarDarkFont(): Boolean = false

    override fun isStatusBarEnabled(): Boolean {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled()
    }

    companion object {
        @JvmStatic
        fun newInstance() = MyMeFragment()
    }
}